<?php
// +----------------------------------------------------------------------
// | author: thinkphp
// +----------------------------------------------------------------------

namespace app\adminapi\logic\user;

use app\common\enum\user\AccountLogEnum;
use app\common\enum\user\UserTerminalEnum;
use app\common\logic\AccountLogLogic;
use app\common\logic\BaseLogic;
use app\common\model\auth\Admin;
use app\common\model\user\User;
use think\facade\Config;
use think\facade\Db;

/**
 * 用户逻辑层
 * Class UserLogic
 * @package app\adminapi\logic\user
 */
class UserLogic extends BaseLogic
{

    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/23 11:42
     */
    public static function add(array $params): bool
    {
        Db::startTrans();


        try {
            $password = "";
            if (empty($params['password'])) {
                $params['password'] = '123456';
            }
            $passwordSalt = Config::get('project.unique_identification');
            $password = create_password($params['password'], $passwordSalt);
            User::create([
                'user_type' => $params['user_type'],
                'travel_agency_id' => $params['travel_agency_id'],
                'avatar' => $params['avatar'],
                'real_name' => $params['real_name'],
                'nickname' => $params['nickname'],
                'account' => $params['account'],
                'password' => $password,
                'mobile' => $params['mobile'],
                'sex' => $params['sex'],
                'is_disable' => $params['is_disable'],
            ]);

            Admin::create([
                'name' => $params['real_name'],
                'account' => $params['account'],
                'avatar' => $params['avatar'],
                'password' => $password,
                'create_time' => time(),
                'disable' => $params['is_disable'],
                'role_id' => 3,
            ]);

            Db::commit();


            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/23 11:42
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {

            if (!empty($params['password'])) {
                $passwordSalt = Config::get('project.unique_identification');
                $password = create_password($params['password'], $passwordSalt);
                User::where('id', $params['id'])->update(['password' => $password]);
                Admin::where('account', $params['account'])->update(['password' => $password]);
            }
            User::where('id', $params['id'])->update([
                'user_type' => $params['user_type'],
                'travel_agency_id' => $params['travel_agency_id'] ?? 0,
                'avatar' => $params['avatar'],
                'real_name' => $params['real_name'],
                'nickname' => $params['nickname'],
                'account' => $params['account'],
                'mobile' => $params['mobile'],
                'sex' => $params['sex'],
                'is_disable' => $params['is_disable'],
            ]);

            Admin::where('account', $params['account'])->update([
                'name' => $params['real_name'],
                'avatar' => $params['avatar'],
                'account' => $params['account'],
                'create_time' => time(),
                'disable' => $params['is_disable'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/23 11:42
     */
    public static function delete(array $params): bool
    {
        return User::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author admin
     * @date 2025/04/23 11:42
     */
    public static function detail($params): array
    {
        return User::findOrEmpty($params['id'])->append(['travel_agency_name'])->toArray();
    }


    /**
     * @notes 更新用户信息
     * @param array $params
     * @return User
     */
    public static function setUserInfo(array $params)
    {
        return User::update([
            'id' => $params['id'],
            $params['field'] => $params['value']
        ]);
    }


    /**
     * @notes 调整用户余额
     * @param array $params
     * @return bool|string
     */
    public static function adjustUserMoney(array $params)
    {
        Db::startTrans();
        try {
            $user = User::find($params['user_id']);
            if (AccountLogEnum::INC == $params['action']) {
                //调整可用余额
                $user->user_money += $params['num'];
                $user->save();
                //记录日志
                AccountLogLogic::add(
                    $user->id,
                    AccountLogEnum::UM_INC_ADMIN,
                    AccountLogEnum::INC,
                    $params['num'],
                    '',
                    $params['remark'] ?? ''
                );
            } else {
                $user->user_money -= $params['num'];
                $user->save();
                //记录日志
                AccountLogLogic::add(
                    $user->id,
                    AccountLogEnum::UM_DEC_ADMIN,
                    AccountLogEnum::DEC,
                    $params['num'],
                    '',
                    $params['remark'] ?? ''
                );
            }

            Db::commit();
            return true;

        } catch (\Exception $e) {
            Db::rollback();
            return $e->getMessage();
        }
    }

}